﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using BookStore.Bussiness;
using BookStore.Data;

namespace BookStore.admin
{
    public partial class Order : System.Web.UI.Page
    {
        static bool flag = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["Administrator"])))
            {
                Response.Redirect("~/admin/Login.aspx");
            }
            showAll();
            if (!Page.IsPostBack)
            {
               loadDDLCustomer();
            loadDDLPayment(); 
            }
        }
        public void showAll()
        {
            gvOrder.DataSource = orderService.getAll();
            gvOrder.DataBind();
        }
        public void loadDDLCustomer() {
            ddlCustomerid.DataSource = customerService.getAll();
            ddlCustomerid.DataTextField = "customerName";
            ddlCustomerid.DataValueField = "customerID";
            ddlCustomerid.DataBind();
        }
        public void loadDDLPayment()
        {
            ddlPaymentid.DataSource = paymentService.getAll();
            ddlPaymentid.DataTextField = "paymentName";
            ddlPaymentid.DataValueField = "paymentID";
            ddlPaymentid.DataBind();
        }
        public void ResetControl()
        {
            txtOrderID.Text ="";
            txtDateorder.Text = "";
            txtDatereceipt.Text = "";
            txtAddress.Text ="";
            txtPhone.Text ="";
            txtTotalMoney.Text ="";
            rdbStatus.Checked =false;
            txtDeliveryCharges.Text ="";   
        }

        public void insertOrder()
        {
            orderInfo objOrder = new orderInfo();
            objOrder.customerID = Convert.ToInt32(ddlCustomerid.SelectedValue);
            objOrder.dateorder = Convert.ToDateTime(txtDateorder.Text);
            objOrder.datereceipt = Convert.ToDateTime(txtDatereceipt.Text);
            objOrder.addressDelivery = txtAddress.Text;
            objOrder.phone = txtPhone.Text;
            objOrder.paymentID =  Convert.ToInt32( ddlPaymentid.SelectedValue);
            objOrder.totalMoney = txtTotalMoney.Text;
            objOrder.status = rdbStatus.Checked;
            objOrder.deliveryCharges = txtDeliveryCharges.Text;
           

            bool test = orderService.insertOrder(objOrder);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('insert success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('insert failed');</script>");
            }
        }
        public void updateOrder()
        {
            orderInfo objOrder = new orderInfo();
            objOrder.orderID = Convert.ToInt32(txtOrderID.Text);
            objOrder.customerID = Convert.ToInt32(ddlCustomerid.SelectedValue);
            objOrder.dateorder = Convert.ToDateTime(txtDateorder.Text);
            objOrder.datereceipt = Convert.ToDateTime(txtDatereceipt.Text);
            objOrder.addressDelivery = txtAddress.Text;
            objOrder.phone = txtPhone.Text;
            objOrder.paymentID = Convert.ToInt32(ddlPaymentid.SelectedValue);
            objOrder.totalMoney = txtTotalMoney.Text;
            objOrder.status = rdbStatus.Checked;
            objOrder.deliveryCharges = txtDeliveryCharges.Text;
            bool test = orderService.updateOrder(objOrder);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('update success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('update failed');</script>");
            }
        }

        protected void lbtAddT_Click(object sender, EventArgs e)
        {
            flag = true;
            ResetControl();
            pnView.Visible = false;
            pnUpdate.Visible = true;
        }

        protected void lbtUpdate_Click(object sender, EventArgs e)
        {
            if (flag == true)
            {
                insertOrder();
            }
            else
            {
                updateOrder();
            }
            showAll();
            pnUpdate.Visible = false;
            pnView.Visible = true;
        }

        protected void lbtRefreshB_Click(object sender, EventArgs e)
        {
            showAll();
        }

        protected void gvOrder_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int OrderID =Convert.ToInt32( gvOrder.Rows[e.RowIndex].Cells[0].Text);
            if (orderService.checkOrder(OrderID)==true)
            {
                Response.Write("<script language='javascript'>alert('orderDetail still existed , dont delete');</script>");
                return;
            }
            bool test = orderService.deleteOrder(OrderID);

            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('delete success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('delete failed');</script>");
            }
            showAll();
        }

        protected void gvOrder_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            flag = false;
            int OrderID = Convert.ToInt32(gvOrder.Rows[e.NewSelectedIndex].Cells[0].Text);
            orderInfo objOrder = orderService.getbyID(OrderID.ToString());
            txtOrderID.Text = objOrder.orderID.ToString();
            
            ddlCustomerid.SelectedValue=objOrder.customerID.ToString();
            txtDateorder.Text = objOrder.dateorder.ToString();
            txtDatereceipt.Text = objOrder.datereceipt.ToString();
            txtAddress.Text =objOrder.addressDelivery ;
            txtPhone.Text =objOrder.phone ;
            ddlPaymentid.SelectedValue=objOrder.paymentID.ToString();
            txtTotalMoney.Text = objOrder.totalMoney.ToString();
            rdbStatus.Checked =objOrder.status;
            txtDeliveryCharges.Text = objOrder.deliveryCharges;        
            pnUpdate.Visible = true;
            pnView.Visible = false;
        }

        protected void lbtBack_Click(object sender, EventArgs e)
        {
            pnView.Visible = true;
            pnUpdate.Visible = false;

        }

        protected void gvOrder_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvOrder.PageIndex = e.NewPageIndex;
            showAll();
        }

    }
}
